# _*_  coding:utf-8 _*_
import re
import threading
import json
import time
import sys

from confluent_kafka.avro import AvroProducer
from confluent_kafka import avro

reload(sys)
sys.setdefaultencoding("utf-8")

item = [
    {"media_id": "12334", "media_content": "第三轮测试", "fetched_time": 1545301058, "media_created_at": -1,
     "media_caption": None},
    {"media_id": "12334", "media_content": "第三轮测试", "fetched_time": 1545301058, "media_created_at": -1,
     "media_caption": u"123"}
]


class WBYiScript(object):
    def __init__(self):
        self.value_schema = {
            'type': 'record',
            'namespace': 'regular.daily.test',
            'name': 'media_info',
            'fields': []
        }
        self.c = threading.RLock()
        self.kafka_config = {'host': ['prd-pg-crawler-kafka-141.weiboyi.com', 'prd-pg-crawler-kafka-142.weiboyi.com',
                                      'prd-pg-crawler-kafka-143.weiboyi.com', 'prd-pg-crawler-kafka-144.weiboyi.com'],
                             'schema_registry_url': 'http://pg-schema-registry.weiboyi.com:28081', 'port': 9092,
                             'batch_count': 100}
        # kafka_config = {'host': ['192.168.1.141', '192.168.1.142', '192.168.1.143'], 'port': 9092, 'schema_registry_url': 'http://pg-schema-registry.weiboyi.com:28081', 'batch_count': 100}
        self.item_count = 0
        self.avroProducer_dict = {}

    def producing(self, item):
        """
        生出json数据标注
        :param item:list类型
        :return: 返回标组完成数据
        """
        fields_list = []
        numbers_dict = {}
        for dict_i in item:
            for key in dict_i.keys():
                if key in numbers_dict.keys():
                    type_ = re.findall("<type '(.*?)'>", str(type(dict_i[key])))[0]
                    if type_ == "str" or type_ == "unicode":
                        type_ = "string"

                    elif type_ == "NoneType":
                        type_ = "null"
                    if type_ == numbers_dict[key]:
                        pass
                    else:
                        lis = [numbers_dict[key], type_]
                        for fields in fields_list:
                            if key == fields["name"]:
                                fields["type"] = lis
                else:
                    type_ = re.findall("<type '(.*?)'>", str(type(dict_i[key])))[0]
                    if type_ == "str" or type_ == "unicode":
                        type_ = "string"
                    elif type_ == "NoneType":
                        type_ = "null"
                    name = key
                    fields_list.append({"type": type_, "name": name})
                    numbers_dict[key] = type_
        self.value_schema["fields"] = fields_list
        print(self.value_schema)
        return self.value_schema


if __name__ == '__main__':
    wby = WBYiScript()
    print(wby.producing(item))
